package com.scau402.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scau402.common.annotation.DataScope;
import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.DateUtils;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.StringUtils;
import com.scau402.common.utils.code.ProcessorCodeUtils;
import com.scau402.system.service.ISysTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scau402.system.mapper.WmsBasicProcessorMapper;
import com.scau402.system.domain.WmsBasicProcessor;
import com.scau402.system.service.IWmsBasicProcessorService;

/**
 * 加工厂Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@Service
@Slf4j
public class WmsBasicProcessorServiceImpl implements IWmsBasicProcessorService {
    @Autowired
    private WmsBasicProcessorMapper wmsBasicProcessorMapper;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询加工厂
     *
     * @param id 加工厂主键
     * @return 加工厂
     */
    @Override
    public WmsBasicProcessor selectWmsBasicProcessorById(Long id) {
        return wmsBasicProcessorMapper.selectById(id);
    }

    /**
     * 查询加工厂列表
     *
     * @param processor 加工厂
     * @return 加工厂
     */
    @Override
    @DataScope(tenantAlias = "t", userAlias = "u")
    public List<WmsBasicProcessor> selectWmsBasicProcessorList(WmsBasicProcessor processor) {
        log.info("selectWmsBasicProcessorList, processor:{}", processor);
        processor.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        // 查询条件为 del_flag = '0' and 其他字段不为null或者''的数据
        LambdaQueryWrapper<WmsBasicProcessor> lambdaQueryWrapper = Wrappers.<WmsBasicProcessor>lambdaQuery()
                .eq(WmsBasicProcessor::getDelFlag, "0")
                .and(StringUtils.isNotBlank(processor.getProcessorCode()), i -> i.eq(WmsBasicProcessor::getProcessorCode, processor.getProcessorCode()))
                .and(StringUtils.isNotBlank(processor.getProcessorName()), i -> i.eq(WmsBasicProcessor::getProcessorName, processor.getProcessorName()))
                .and(StringUtils.isNotBlank(processor.getProcessorType()), i -> i.eq(WmsBasicProcessor::getProcessorType, processor.getProcessorType()))
                .and(StringUtils.isNotBlank(processor.getContactPersonName()), i -> i.eq(WmsBasicProcessor::getContactPersonName, processor.getContactPersonName()))
                .and(StringUtils.isNotBlank(processor.getContactPersonPhone()), i -> i.eq(WmsBasicProcessor::getContactPersonPhone, processor.getContactPersonPhone()))
                .and(StringUtils.isNotNull(processor.getDefaulttime()), i -> i.eq(WmsBasicProcessor::getDefaulttime, processor.getDefaulttime()))
                .and(StringUtils.isNotBlank(processor.getProcessortime()), i -> i.eq(WmsBasicProcessor::getProcessortime, processor.getProcessortime()))
                .and(StringUtils.isNotNull(processor.getSettlementCycle()), i -> i.eq(WmsBasicProcessor::getSettlementCycle, processor.getSettlementCycle()))
                .and(StringUtils.isNotBlank(processor.getLendAmountLimit()), i -> i.eq(WmsBasicProcessor::getLendAmountLimit, processor.getLendAmountLimit()))
                .and(StringUtils.isNotBlank(processor.getCreatename()), i -> i.eq(WmsBasicProcessor::getCreatename, processor.getCreatename()))
                .and(StringUtils.isNotBlank(processor.getCreateaccount()), i -> i.eq(WmsBasicProcessor::getCreateaccount, processor.getCreateaccount()))
                .and(StringUtils.isNotBlank(processor.getCreatebank()), i -> i.eq(WmsBasicProcessor::getCreatebank, processor.getCreatebank()))
                .and(StringUtils.isNotBlank(processor.getStatus()), i -> i.eq(WmsBasicProcessor::getStatus, processor.getStatus()))
                .and(StringUtils.isNotNull(processor.getTenantId()), i -> i.eq(WmsBasicProcessor::getTenantId, processor.getTenantId()))
                .and(StringUtils.isNotNull(processor.getDeptId()), i -> i.eq(WmsBasicProcessor::getDeptId, processor.getDeptId()));
        return wmsBasicProcessorMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 新增加工厂
     *
     * @param processor 加工厂
     * @return 结果
     */
    @Override
    public int insertWmsBasicProcessor(WmsBasicProcessor processor) {
        log.info("insertWmsBasicProcessor, processor:{}", processor);
        processor.setCreateTime(DateUtils.getNowDate());
        processor.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        return wmsBasicProcessorMapper.insert(processor);
    }

    /**
     * 修改加工厂
     *
     * @param processor 加工厂
     * @return 结果
     */
    @Override
    public int updateWmsBasicProcessor(WmsBasicProcessor processor) {
        log.info("updateWmsBasicProcessor, processor:{}", processor);
        processor.setUpdateTime(DateUtils.getNowDate());
        LambdaUpdateWrapper<WmsBasicProcessor> lambdaUpdateWrapper = Wrappers.<WmsBasicProcessor>lambdaUpdate()
                .eq(WmsBasicProcessor::getId, processor.getId())
                .set(StringUtils.isNotBlank(processor.getProcessorCode()), WmsBasicProcessor::getProcessorCode, processor.getProcessorCode())
                .set(StringUtils.isNotBlank(processor.getProcessorName()), WmsBasicProcessor::getProcessorName, processor.getProcessorName())
                .set(StringUtils.isNotBlank(processor.getProcessorType()), WmsBasicProcessor::getProcessorType, processor.getProcessorType())
                .set(StringUtils.isNotBlank(processor.getContactPersonName()), WmsBasicProcessor::getContactPersonName, processor.getContactPersonName())
                .set(StringUtils.isNotBlank(processor.getContactPersonPhone()), WmsBasicProcessor::getContactPersonPhone, processor.getContactPersonPhone())
                .set(StringUtils.isNotNull(processor.getDefaulttime()), WmsBasicProcessor::getDefaulttime, processor.getDefaulttime())
                .set(StringUtils.isNotBlank(processor.getProcessortime()), WmsBasicProcessor::getProcessortime, processor.getProcessortime())
                .set(StringUtils.isNotNull(processor.getSettlementCycle()), WmsBasicProcessor::getSettlementCycle, processor.getSettlementCycle())
                .set(StringUtils.isNotBlank(processor.getLendAmountLimit()), WmsBasicProcessor::getLendAmountLimit, processor.getLendAmountLimit())
                .set(StringUtils.isNotBlank(processor.getCreatename()), WmsBasicProcessor::getCreatename, processor.getCreatename())
                .set(StringUtils.isNotBlank(processor.getCreateaccount()), WmsBasicProcessor::getCreateaccount, processor.getCreateaccount())
                .set(StringUtils.isNotBlank(processor.getCreatebank()), WmsBasicProcessor::getCreatebank, processor.getCreatebank())
                .set(StringUtils.isNotBlank(processor.getRemark()), WmsBasicProcessor::getRemark, processor.getRemark())
                .set(StringUtils.isNotBlank(processor.getStatus()), WmsBasicProcessor::getStatus, processor.getStatus())
                .set(StringUtils.isNotNull(processor.getTenantId()), WmsBasicProcessor::getTenantId, processor.getTenantId())
                .set(StringUtils.isNotNull(processor.getDeptId()), WmsBasicProcessor::getDeptId, processor.getDeptId());
        return wmsBasicProcessorMapper.update(null, lambdaUpdateWrapper);
    }

    /**
     * 批量删除加工厂
     *
     * @param ids 需要删除的加工厂主键
     * @return 结果
     */
    @Override
    public int deleteWmsBasicProcessorByIds(Long[] ids) {
        log.info("deleteWmsBasicProcessorByIds, ids:{}", ids);
        LambdaUpdateWrapper<WmsBasicProcessor> lambdaUpdateWrapper = Wrappers.<WmsBasicProcessor>lambdaUpdate()
                .in(WmsBasicProcessor::getId, ids)
                .set(WmsBasicProcessor::getDelFlag, "2");
        return wmsBasicProcessorMapper.update(null, lambdaUpdateWrapper);
    }

    /**
     * 删除加工厂信息
     *
     * @param id 加工厂主键
     * @return 结果
     */
    @Override
    public int deleteWmsBasicProcessorById(Long id) {
        log.info("deleteWmsBasicProcessorById, id:{}", id);
        return wmsBasicProcessorMapper.deleteById(id);
    }

    @Override
    public String getMaxCode(SysUser user) {
        log.info("getMaxCode, user:{}", user);
        Long tenantId = user.getTenantId();
        return wmsBasicProcessorMapper.getMaxCode(tenantId);
    }

    @Override
    public String getProcessorCode(String loginUserTenantCode) throws InterruptedException {
        log.info("getProcessorCode, loginUserTenantCode:{}", loginUserTenantCode);
        ProcessorCodeUtils processorCodeUtils = new ProcessorCodeUtils();
        return processorCodeUtils.getNewCode(loginUserTenantCode);
    }

    @Override
    public String getNewCode(SysUser user) throws InterruptedException {
        log.info("getNewCode, user:{}", user);
        // 获取数据库中最大的编码
        String maxCode = getMaxCode(user);

        // 返回maxCode为NULL，及当前数据库没有数据，从0开始
        if (StringUtils.isNull(maxCode)) {
            maxCode = "0";
        }

        // 获取当前用户所属租户编码
        Long loginUserTenantId = SecurityUtils.getLoginUser().getTenantId();
        String loginUserTenantCode = tenantService.selectSysTenantByTenantId(loginUserTenantId).getTenantCode();

        // 设置redis的当前编码
        ProcessorCodeUtils processorCodeUtils = new ProcessorCodeUtils();
        processorCodeUtils.setCurrentCodeToMax(maxCode, loginUserTenantCode);
        return processorCodeUtils.getNewCode(loginUserTenantCode);
    }
}
